(function($){

  function CdrTable(container){
    var data = new Array;
    win_abs_top = 0;
    win_length = 0;
    cdr_table = this;
    init_tool_bar();
    this.insert = insert_cdr_row;
    return this;
    function insert_cdr_row(row){
      data.push(row);
      build_data_row(row);
      win_abs_top ++;
    }
    function build_data_row(cdr){
      if(win_length >= 10){
        $(container + " table tr:last-child").remove();
        win_length -- ;
      }
      $(container + " table").prepend(cdr_to_row(cdr));
      win_length ++;
      $(container).resizable( "option", "minHeight", $(container + " table").height() + 20 );
    }
    function cdr_to_row(Cdr){
	    return $("<tr></tr>")
		                .append("<td>" + Cdr.rrc_con_reason + "</td>")
                    .append("<td>" + Cdr.user_id + "</td>")
		                .append("<td>" + Cdr.start_time + "</td>")
                    .append("<td>" + Cdr.end_time + "</td>")
                    .append("<td>" + Cdr.aux_info + "</td>")
                    .append("<td>" + Cdr.rab_info + "</td>")
		                .append("<td>" + Cdr.end_reason + "</td>");
    }
    function init_tool_bar(){
      $("<div></div>")
          .addClass("tool_bar")
          .append("<button id='prev'>prev</button>")
          .append("<button id='next'>next</button>")
          .appendTo(container);
      $(container + " div.tool_bar button#prev").click(function(){show_prev_page();});
      $(container + " div.tool_bar button#next").click(function(){show_next_page();});
    }
    function show_next_page(){
      if(win_abs_top >= data.length)return;
      win_abs_top = data.length > win_abs_top + 10 ? win_abs_top + 10 : data.length;
      refresh();
    }
    function show_prev_page(){
      if((win_abs_top - 10) <= 0) return;
      win_abs_top -= 10;
      refresh();
    }
    function refresh(){
      var i = 0;
      $(container + " table").empty();
      win_length = 0;
      for(i = win_abs_top; (i > 0) && (i > win_abs_top - 10); i --){
        $(container + " table").append(cdr_to_row(data[i-1]));
        win_length ++;
      }
    }
  }
  $.cdr_table = function(container){
    var table = new CdrTable(container);
    return table;
  }
  $.fn.cdr_table = function(){
    return this.each(function(){
        $.cdr_table(this);
      });
  }

  function NETable(container){
    var data = new Array;
    ne_table = this;
    this.insert = insert_ne_row;
    this.setData = set_data;
    return this;
    function set_data(new_data){
      data = new_data;
      refresh();
    }
    function refresh(){
      $(container + " table").empty();
      $.each(data,function(index,item){
                    build_data_row(item);
                  });
    }
    function insert_ne_row(row){
      data.push(row);
      build_data_row(row);
    }
    function build_data_row(ne){
      $(container + " table").append(ne_to_row(ne));
    }
    function ne_to_row(NE){
	    return $("<tr></tr>")
		                .append("<td>" + NE.addr + "</td>")
		                .append("<td>" + NE.role + "</td>")
		                .append("<td>" + NE.send + "</td>")
                    .append("<td>" + NE.recv + "</td>");
    }
  }
  $.ne_table = function(container){
    var table = new NETable(container);
    return table;
  }
  $.fn.ne_table = function(){
    return this.each(function(){
        $.ne_table(this);
      });
  }

})(jQuery);
